package com.zimin.cloud.wiki.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zimin.cloud.mine.pojo.wiki.business.EbookBO;
import com.zimin.cloud.mine.pojo.wiki.view.EbookVO;
import com.zimin.cloud.wiki.mapper.EbookMapper;
import com.zimin.cloud.wiki.pojo.entity.Ebook;
import com.zimin.cloud.wiki.service.IEbookService;
import com.zimin.common.utils.BeanUtils;
import com.zimin.common.utils.StringUtils;
import com.zimin.shared.pojo.PageVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 电子书 服务实现类
 * </p>
 *
 * @author zimin
 * @since 2021-10-27 21:02
 */
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook> implements IEbookService {

    @Override
    public PageVO<EbookVO> list(Page<Ebook> page, EbookBO ebookBO) {
        Page<Ebook> ebookPage = page(page, Wrappers.<Ebook>lambdaQuery()
                .eq(StringUtils.isNotEmpty(ebookBO.getCategory()),Ebook::getCategory,ebookBO.getCategory())
                .eq(StringUtils.isNotEmpty(ebookBO.getSecondary()),Ebook::getSecondary,ebookBO.getSecondary())
                .like(StringUtils.isNotEmpty(ebookBO.getName()), Ebook::getName, ebookBO.getName())
                .orderByDesc(Ebook::getCreateTime));
        List<EbookVO> ebookVOS = BeanUtils.listEntityToView(ebookPage.getRecords(), EbookVO.class);
        return pageToPageVO(ebookPage,ebookVOS);
    }

    @Override
    public boolean updateById(EbookBO ebookBO) {
        var ebook = new Ebook();
        BeanUtils.copyProperties(ebookBO, ebook);
        return updateById(ebook);
    }

    @Override
    public boolean save(EbookBO ebookBO) {
        var ebook = new Ebook();
        BeanUtils.copyProperties(ebookBO, ebook);
        return save(ebook);
    }
}
